package com.py.framework.core.base.controller;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.common.SpringContextHolder;
import com.py.framework.core.rest.support.RestServiceResult;

public class AdvanceSearchController<S> implements InitializingBean {

	private S service;

	private String FRONT_TABLE_ID;

	@RequestMapping(value = "/getOptionalColumns.json")
	public RestServiceResult<Object> getOptionalColumns() throws Exception {

		Method method = service.getClass().getMethod("getOptionalColumns");

		return new RestServiceResult<Object>(method.invoke(service));
	}

	@RequestMapping(value = "/getAdvancedOptionalColumns.json")
	public RestServiceResult<Object> getAdvancedOptionalColumns() throws Exception {

		Method method = service.getClass().getMethod("getAdvancedOptionalColumns");

		return new RestServiceResult<Object>(method.invoke(service));
	}

	@IgnoreAuthority
	@RequestMapping(value = "/getListByConditionForPage.json")
	public Object getListByCondition(@RequestParam Map<String, Object> params,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page)
			throws Exception {

		Method method = service.getClass().getMethod("getListByConditionForPage", Map.class, String.class, Page.class,
				String.class);
		Object invoke = null;
		try {
			invoke = method.invoke(service, params, condition, page, FRONT_TABLE_ID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RestServiceResult<Object>(page.getTotalCount(), invoke);
	}

	@RequestMapping(value = "/export.json")
	public void write2Excel(@RequestParam Map<String, Object> params,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN) throws Exception {
		Method method = service.getClass().getMethod("write2Excel", Map.class, String.class, String.class, Page.class,
				String.class, String.class);
		method.invoke(service, params, condition, columnEN, page, "导出列表", "导出列表");

	}

	@RequestMapping(value = "/addShowColumns.json")
	public RestServiceResult<Object> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns)
			throws Exception {

		Method method = service.getClass().getMethod("addShowColumns", String[].class, String.class);

		method.invoke(service, columns, FRONT_TABLE_ID);
		return new RestServiceResult<>();
	}

	/**
	 * 获取类泛型class
	 *
	 * @return
	 */

	@SuppressWarnings("unchecked")
	public Class<S> getServiceClass() {
		return (Class<S>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	@Override
	public void afterPropertiesSet() throws Exception {

		Class<S> mapperClass = getServiceClass();

		this.FRONT_TABLE_ID = mapperClass.getName() + "_" + RandomStringUtils.randomAlphabetic(10);

		this.service = SpringContextHolder.getBean(mapperClass);

	}

}
